package org.flintparticles.twoD.stage3d.images
{
	import flash.display.BitmapData;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.display3D.textures.TextureBase;
	
	import org.flintparticles.common.stage3d.Vao;
	import org.flintparticles.common.stage3d.utils.Stage3DMath;

	public class Image2D extends Polygon2D
	{
		private var  alphaVector:Vector.<Number> = new <Number>[alpha, alpha, alpha, alpha];
		private var _texture:TextureBase;
	
		private var _width:Number;
		private var _height:Number;
		private var _btmData:BitmapData;
		public function Image2D(texture:TextureBase,width:Number,height:Number)
		{
			
			 _height=height;
			 _width=width;
			
			super(_width,_height);
			_vao.premultipliedAlpha=false;
			_vao.setTexCoords(0, 0.0, 0.0);
			_vao.setTexCoords(1, 1.0, 0.0);
			_vao.setTexCoords(2, 0.0, 1.0);
			_vao.setTexCoords(3, 1.0, 1.0);
		
		  _texture=texture;	
		  pivot.x=width/2;
		  pivot.y=height/2;
		  
		}
		public override function draw(stageMath:Stage3DMath):void{
			//_context.clear();
			alphaVector= new <Number>[alpha, alpha, alpha, alpha];
			
			
			
			if (_context == null) throw new Error("The context3D is not set!!!");
			
			var pma:Boolean = _vao.premultipliedAlpha;
			var alphaVector:Vector.<Number> = pma ? new <Number>[alpha, alpha, alpha, alpha] :
				new <Number>[1.0, 1.0, 1.0, alpha];
			
			
			stageMath.pushMatrix();
			stageMath.transformMatrix(this );
			///setting buffer for vertices///
			
			///setting transformation matrix uniform
			//	trace("MVP Matrix is:"+stageMath.mvpMatrix.position);
			//_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, stageMath.mvpMatrix, true);
			/////changed to :now passing model and projection matrices to GPU to offload CPU calcualtions,the MVP result matrix is
			////calcualted in the Vertex Shader///
		//	_context.setTextureAt(1,_texture);
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, stageMath.mModelViewMatrix, true); ///stageMath.mvpMatrix
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 4, stageMath.mProjectionMatrix, true); 
			///setting alpha uniform
			/////upload alpha/////
			_context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 0, alphaVector, 1);
			
		
			
			
			//drawing///
			_context.drawTriangles(_indexBuffer, 0, 2);
			
			////unbound buffers////////
			//	_context.setVertexBufferAt(0, null);
			//	_context.setVertexBufferAt(1, null);
		//	_context.setTextureAt(1, null);
			stageMath.popMatrix();
		}
		public override function set context(value:Context3D):void
		{
			_context = value;
			if (_vertexBuffer == null) createVertexBuffer();
			if (_indexBuffer  == null) createIndexBuffer();
			_context.setVertexBufferAt(0, _vertexBuffer, Vao.POSITION_OFFSET, Context3DVertexBufferFormat.FLOAT_3);
			_context.setVertexBufferAt(1, _vertexBuffer, Vao.COLOR_OFFSET,    Context3DVertexBufferFormat.FLOAT_4);
			_context.setVertexBufferAt(2, _vertexBuffer, Vao.TEXCOORD_OFFSET, Context3DVertexBufferFormat.FLOAT_2);
			_context.setTextureAt(1,_texture);
			setDefaultBlendFactors(_vao.premultipliedAlpha);
		}
		public override function dispose():void{
			if(_vertexBuffer)_vertexBuffer.dispose();
			if(_indexBuffer)_indexBuffer.dispose();
			super.dispose();
			
		}
		
	
		
		
	
		
		public override function clone():Polygon2D{
			var clone:Image2D=new Image2D(_texture,width,height);
	
			clone.vertexData=vertexData;
			clone.shaderProgram=this.shaderProgram;
			clone.context=this.context;
			return clone;
			
		}

		public function get height():Number
		{
			return _height;
		}

		public function get width():Number
		{
			return _width;
		}


		////////////////////buffers init///////////////////////////////
		
		
		
		

	}
}